package com.atguigu.gulimall.product.service.impl;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.product.dao.SkuInfoDao;
import com.atguigu.gulimall.product.entity.SkuInfoEntity;
import com.atguigu.gulimall.product.service.SkuInfoService;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveSkuInfo(SkuInfoEntity skuInfoEntity) {
        this.save(skuInfoEntity);
    }

    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {
        QueryWrapper<SkuInfoEntity> queryWrapper = new QueryWrapper<>();
        //key:
        //catelogId: 0
        //brandId: 0
        //min: 0
        //max: 0
        String key = params.get("key") != null ? (String) params.get("key") : "";
        if (StringUtils.isNotBlank(key)) {
            queryWrapper.and(keyQueryWrapper -> {
                keyQueryWrapper.eq("sku_id", key)
                        .or()
                        .like("sku_name", key);
            });
        }

        String catelogId = params.get("catelogId") != null ? (String) params.get("catelogId") : "";
        if (StringUtils.isNotBlank(catelogId)) {
            //如果是数字格式 ^\d{n,}$
            boolean isMatch = Pattern.matches("^\\d{1,}$", catelogId);
            if (isMatch && Long.parseLong(catelogId) > 0) {
                queryWrapper.eq("catalog_id", catelogId);
            }
        }

        String brandId = params.get("brandId") != null ? (String) params.get("brandId") : "";
        if (StringUtils.isNotBlank(brandId)) {
            //如果是数字格式 ^\d{n,}$
            boolean isMatch = Pattern.matches("^\\d{1,}$", brandId);
            if (isMatch && Long.parseLong(brandId) > 0) {
                queryWrapper.eq("brand_id", brandId);
            }
        }

        String min = params.get("min") != null ? (String) params.get("min") : "";
        if (StringUtils.isNotBlank(min)) {
            //ge greater equals
            //gt greater than
            queryWrapper.ge("price", min);
        }

        String max = params.get("max") != null ? (String) params.get("max") : "";
        if (StringUtils.isNotBlank(max)) {
            //如果是数字格式 ^\d{n,}$
            boolean isMatch = Pattern.matches("^\\d{1,}$", max);
            if (isMatch) {
                boolean isValid = BigDecimal.ZERO.compareTo(new BigDecimal(max)) == -1;
                if (isValid) {
                    //le less equals
                    //lt less than
                    queryWrapper.le("price", max);
                }
            }
        }

        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public List<SkuInfoEntity> getSkusBySpuId(Long spuId) {
        return this.baseMapper.selectList(new QueryWrapper<SkuInfoEntity>().eq("spu_id", spuId));
    }

}